// 首页
// import { isEmpty } from 'lodash'
import * as types from '@/store/mutation-types'
import api from '@/tools/api.js'
import promise from '@/tools/promise'
// import storage from '@/tools/storage'
import { SUCCESS_STATUS } from '@/config/status'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  pageData2: {
    total: 1,
    per_page: 2,
    current_page: 1,
    last_page: 1
  },
  certiIncreTendency: {},
  certificateList: [],
  certificateInfo: {},
  transferList: [],
  invoiceList: [],
  pickUpList: [],
  distributeList: [],
  distributePageList: [],
  distributeRuleList: [],
  saveData: {}
}

// mutations
const mutations = {
  [types.CERTI_INCRE_TENDENCY] (state, payload) {
    state.certiIncreTendency = payload
  },
  [types.CERTIFICATE_LIST] (state, payload) {
    state.certificateList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.CERTIFICATE_INFO] (state, payload) {
    state.certificateInfo = payload
  },
  [types.CERTIFICATE_TRANSFER] (state, payload) {
    state.transferList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.CERTIFICATE_INVOICE] (state, payload) {
    state.invoiceList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.CERTIFICATE_PICKUP] (state, payload) {
    state.pickUpList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.CERTIFICATE_SAVE_DATA] (state, payload) {
    state.saveData = payload
  },
  [types.DISTRIBUTE_PAGE_LIST] (state, payload) {
    state.distributePageList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.DISTRIBUTE_RULE_LIST] (state, payload) {
    state.distributeRuleList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.DISTRIBUTE_LIST] (state, payload) {
    state.distributeList = payload.data
    state.certificate_ids = payload.certificate_ids
    state.pageData2.total = Number(payload.total)
    state.pageData2.per_page = Number(payload.per_page)
    state.pageData2.current_page = Number(payload.current_page)
    state.pageData2.last_page = Number(payload.last_page)
  }
}

// actions
const actions = {
  async certiIncreTendency ({ commit }, payload) {
    let getRes = await api('servercertificate/certificate/Certificate/certiIncreTendency')
    SUCCESS_STATUS === getRes.status && commit(types.CERTI_INCRE_TENDENCY, getRes.res)
    return promise(getRes)
  },
  // 证书列表
  async certificateList ({ commit }, payload) {
    let listRes = await api('servercertificate/certificate/Certificate/adminAllCertiList', payload)
    commit(types.CERTIFICATE_LIST, listRes.res)
  },
  // 证书作废
  async certificateCancel ({ commit }, payload) {
    let cancelRes = await api('servercertificate/certificate/Certificate/invalid', payload)
    return promise(cancelRes)
  },
  // 证书详情
  async certificateInfo ({ commit }, payload) {
    let infoRes = await api('servercertificate/certificate/Certificate/certificateInfo', payload)
    commit(types.CERTIFICATE_INFO, infoRes.res)
  },
  // 证书转让记录
  async certificateTransfer ({ commit }, payload) {
    let transferRes = await api('servercertificate/certificate/Certificate/getTransferList', payload)
    commit(types.CERTIFICATE_TRANSFER, transferRes.res)
  },
  // 证书审核
  async certificateAudit ({ commit }, payload) {
    let auditRes = await api('servercertificate/certificate/Certificate/auditOneCertificate', payload)
    return promise(auditRes)
  },
  // 证书发票
  async invoiceList ({ commit }, payload) {
    let listRes = await api('servercertificate/certificate/Certificate/invoiceList', payload)
    commit(types.CERTIFICATE_INVOICE, listRes.res)
  },
  // 证书提货单
  async pickupList ({ commit }, payload) {
    let listRes = await api('servercertificate/certificate/Certificate/pickUpGoodsList', payload)
    commit(types.CERTIFICATE_PICKUP, listRes.res)
  },
  // 证书发货
  async shipments ({ commit }, payload) {
    let res = await api('servercertificate/certificate/Certificate/shipmentsGoods', payload)
    return promise(res)
  },
  // 证书积分派发记录
  async distributePageList ({ commit }, payload) {
    let listRes = await api('serverfund/integral/IntegralDistribute/getDistributePageList', payload)
    commit(types.DISTRIBUTE_PAGE_LIST, listRes.res)
  },
  // 证书积分派发规则记录
  async distributeRuleList ({ commit }, payload) {
    let listRes = await api('serverfund/integral/IntegralDistribute/list', payload)
    commit(types.DISTRIBUTE_RULE_LIST, listRes.res)
  },
  // 证书积分派发规则添加
  async distributeRuleAdd ({ commit }, payload) {
    let addRes = await api('serverfund/integral/IntegralDistribute/add', payload)
    return promise(addRes)
  },
  // 派发证书列表
  async distributeList ({ commit }, payload) {
    let listRes = await api('servercertificate/certificate/Certificate/getNeedDistributeList', payload)
    commit(types.DISTRIBUTE_LIST, listRes.res)
    return promise(listRes)
  },
  // 派发证书列表
  async OneKeyDistribute ({ commit }, payload) {
    let listRes = await api('serverfund/integral/IntegralDistribute/oneKeyDistribute', payload)
    return promise(listRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
